package com.lhj.xxsc.seckill.scheduled;

import com.lhj.xxsc.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2021/1/12 23:03
 * <p>
 * 秒杀商品的定时上架服务；
 * 每天晚上 3点；上架最近三天需要秒杀的商品。
 * 当天 00:00:00 - 23:59:59
 * 明天 00:00:00 - 23:59:59
 * 后天 00:00:00 - 23:59:59
 */
@Slf4j
@Service
public class SeckillSkuScheduled {
	@Autowired
	SeckillService seckillService;
	@Autowired
	RedissonClient redissonClient;

	private static final String upload_lock = "seckill:upload:lock";

	@Scheduled(cron = "0 0 3 * * ?")
	public void uploadSeckillSkuLatest3Days() {
		log.info("！！！秒杀上架");
		RLock lock = redissonClient.getLock(upload_lock);
		lock.lock(20, TimeUnit.SECONDS);
		try {
			seckillService.uploadSeckillSkuLatest3Days();
		} finally {
			lock.unlock();
		}
	}
}
